package day_2022_12_to_3.year2023;
import java.util.*;
/**
 * @author haomin
 * @date 2023/04/07 23:28
 **/
public class Day4_7_Num1040 {
    class Solution {
        public int[] numMovesStonesII(int[] stones) {

            Arrays.sort(stones);

            int[] result = new int[]{stones.length, 0};
            result[1] = Math.max(stones[stones.length - 1] - stones[1], stones[stones.length - 2] - stones[0]) - stones.length + 2;

            for (int i = 0, j = 0; i < stones.length; i++) {
                while (j < stones.length && stones[j] - stones[i] + 1 <= stones.length) {
                    j++;
                }
                if (j - i == stones.length - 1 && stones[j - 1] - stones[i] + 1 == stones.length - 1) {
                    result[0] = Math.min(result[0], 2);
                } else {
                    result[0] = Math.min(result[0], stones.length - (j - i));
                }
            }

            return result;

        }
    }
}